package com.xmy.cultivate.listeners;

import com.xmy.cultivate.constants.MqConstants;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.entity.Student;
import com.xmy.cultivate.entity.StudentFaceData;
import com.xmy.cultivate.entity.StudentFaceManyDetail;
import com.xmy.cultivate.service.IStaffService;
import com.xmy.cultivate.service.IStudentFaceManyDetailService;
import com.xmy.cultivate.service.IStudentFaceManyService;
import com.xmy.cultivate.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component
public class FaceListener {

    @Autowired
            @Lazy
    IStudentFaceManyDetailService iStudentFaceManyDetailService;

    @Autowired
    @Lazy
    IStudentFaceManyService iStudentFaceManyService;

    @Autowired
    @Lazy
    IStudentService iStudentService;

    @Autowired
    @Lazy
    IStaffService iStaffService;



    /*@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_TEST_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_TEST_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void test2(String id){
        System.out.println("id2---"+id);
    }*/


    /**********************************************************************************************/
    //以下使用多个队列，是为了让每个队列处理各自的人脸帐号信息，防止同时处理超过人脸的速率限制

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY1
    ))
    public void ADD_FACE_MANY_DETAIL_Q1(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY2
    ))
    public void ADD_FACE_MANY_DETAIL_Q2(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY3
    ))
    public void ADD_FACE_MANY_DETAIL_Q3(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY4
    ))
    public void ADD_FACE_MANY_DETAIL_Q4(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY5
    ))
    public void ADD_FACE_MANY_DETAIL_Q5(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY6
    ))
    public void ADD_FACE_MANY_DETAIL_Q6(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY7
    ))
    public void ADD_FACE_MANY_DETAIL_Q7(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY8
    ))
    public void ADD_FACE_MANY_DETAIL_Q8(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY9
    ))
    public void ADD_FACE_MANY_DETAIL_Q9(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY10
    ))
    public void ADD_FACE_MANY_DETAIL_Q10(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY11
    ))
    public void ADD_FACE_MANY_DETAIL_Q11(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY12
    ))
    public void ADD_FACE_MANY_DETAIL_Q12(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY13
    ))
    public void ADD_FACE_MANY_DETAIL_Q13(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY14
    ))
    public void ADD_FACE_MANY_DETAIL_Q14(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY15
    ))
    public void ADD_FACE_MANY_DETAIL_Q15(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY16
    ))
    public void ADD_FACE_MANY_DETAIL_Q16(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY17
    ))
    public void ADD_FACE_MANY_DETAIL_Q17(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    /*@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY18
    ))
    public void ADD_FACE_MANY_DETAIL_Q18(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY19
    ))
    public void ADD_FACE_MANY_DETAIL_Q19(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_FACE_MANY_DETAIL_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_FACE_MANY_DETAIL_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_ALY20
    ))
    public void ADD_FACE_MANY_DETAIL_Q20(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        boolean rs = iStudentFaceManyService.addFaceDataMany(studentFaceManyDetail);
        if (!rs){
            throw new RuntimeException("消费失败");
        }
    }*/


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_DEL_STUDENT_FACE_DATA_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_DEL_STUDENT_FACE_DATA_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void delStudentFaceData(Long studentId) throws Exception {
        Student student = iStudentService.getById(studentId);
        Integer faceType = 1;
        if (!Objects.isNull(student)){
            iStudentFaceManyDetailService.delStudentFaceData(studentId,faceType);
        }else {
            Staff staff = iStaffService.getById(studentId);
            if (!Objects.isNull(staff)){
                faceType =2;
                iStudentFaceManyDetailService.delStudentFaceData(studentId,faceType);
            }
        }

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_UPDATE_STUDENT_FACE_DATA_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_UPDATE_STUDENT_FACE_DATA_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void updateStudentFaceData(Long studentId) throws Exception {
        Student student = iStudentService.getById(studentId);
        Integer faceType = 1;
        if (!Objects.isNull(student)){
            iStudentFaceManyDetailService.updateStudentFaceData(studentId,faceType);
        }else {
            Staff staff = iStaffService.getById(studentId);
            if (!Objects.isNull(staff)){
                faceType =2;
                iStudentFaceManyDetailService.updateStudentFaceData(studentId,faceType);
            }
        }
    }
}
